﻿using Ken_WPF_UI.Model;
using Ken_WPF_UI.ViewModel;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
//using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// MediaElementWindow3.xaml 的交互逻辑
    /// </summary>
    public partial class MediaElementWindow3 : Window
    {

        private Timer timer;

        public MediaElementWindow3()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;

            MediaConfigInfo mediaConfig = new MediaConfigInfo() { Width = myDockPanel.ActualWidth- 20, Height = myDockPanel.ActualHeight };
            this.bindBorder.DataContext = mediaConfig;

            CanvasInfo playPointInfo = new CanvasInfo() { Left=10,Top= bindBorder.ActualHeight+10};

            CanvasInfo playStatusPointInfo = new CanvasInfo() { Left = 10, Top = bindBorder.ActualHeight + playInfo.ActualHeight + 10 };

            CanvasInfo playImgPointInfo = new CanvasInfo() { Left = rightMainDockPanel.ActualWidth / 2, Top = (rightMainDockPanel.ActualHeight-140)/2 };

            this.playInfo.DataContext = playPointInfo;
            this.playBtnInfo.DataContext = playStatusPointInfo;
            this.playStackPanel.DataContext = playImgPointInfo;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                posSlider.Value = me.Position.TotalSeconds;
                Console.WriteLine("进度值:" + posSlider.Value);
                SetTime();
            }));
        }

        private void SetTime()
        {
            lbTime.Content = string.Format("{0:00}:{1:00}:{2:00}"
                , me.Position.Hours, me.Position.Minutes, me.Position.Seconds);
        }

        private void openBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = @"视频文件(*avi格式)|*.avi|视频文件(*mp4格式)|*.mp4|视频文件(*wmv格式)|*.wmv";

            if (openFileDialog.ShowDialog() != false)
            {
                string fileName = openFileDialog.FileName;
                me.Source = new Uri(fileName, UriKind.Absolute);
                playBtn.IsEnabled = true;
                me.Play();
                timer.Start();
                playBtn.Content = "暂停";
            }

        }

        /// <summary>
        /// 播放或暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void playBtn_Click(object sender, RoutedEventArgs e)
        {
            SetPlayer(true);
            PlayOrPause();
        }


        /// <summary>
        /// 设置下面控件的可用性
        /// </summary>
        /// <param name="status"></param>
        private void SetPlayer(bool status)
        {
            this.openBtn.IsEnabled = status;
            this.playBtn.IsEnabled = status;
            this.stopBtn.IsEnabled = status;
            this.backBtn.IsEnabled = status;
            this.forwardBtn.IsEnabled = status;
        }

        /// <summary>
        /// 播放或暂停
        /// </summary>
        private void PlayOrPause()
        {
            if (playBtn.Content.ToString() == "暂停")
            {
                me.Pause();
                timer.Stop();
                playBtn.Content = "播放";
                //me.ToolTip = "单击播放";
                playStackPanel.Visibility = Visibility.Visible;
            }
            else
            {
                me.Play();
                timer.Start();
                playBtn.Content = "暂停";
                //me.ToolTip = "单击暂停";
                playStackPanel.Visibility = Visibility.Hidden;
                SetTime();
            }
        }


        /// <summary>
        /// 停止播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void stopBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Stop();
            posSlider.Value = 0;
            lbTime.Content = "0:0:0";
            playBtn.Content = "播放";
            timer.Stop();
            playStackPanel.Visibility = Visibility.Visible;
        }


        /// <summary>
        /// 快进
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void forwardBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position + TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 快退
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position - TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 点击播放器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PlayOrPause();
        }

        /// <summary>
        /// ，媒体资源加载完毕后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MediaOpened(object sender, RoutedEventArgs e)
        {
            posSlider.Maximum = me.NaturalDuration.TimeSpan.TotalSeconds;
            lbTime.Content = "0:0:0";
            SetPlayer(true);
        }

        /// <summary>
        /// 滑块鼠标左键拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void posSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            me.Pause();
            timer.Stop();
            int val = (int)posSlider.Value;
            me.Position = new TimeSpan(0, 0, 0, val);
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 播放结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MediaEnded(object sender, RoutedEventArgs e)
        {
            me.Stop();
            timer.Stop();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //图像文件(*.bmp, *.jpg) | *.bmp; *.jpg |
            openFileDialog.Filter = @"视频文件(*avi格式,*wmv格式,*mp4格式)| *.avi;*.wmv;*.mp4";
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() != false)
            {
                //添加到列表
                SingViewModel singViewModel = new SingViewModel();

                string[] fileNames = openFileDialog.FileNames;
                foreach (var item in fileNames)
                {
                    singViewModel.SingList.Add(new SingInfo() {SingName =Path.GetFileName(item),SingLocation=Path.GetDirectoryName(item)});
                }

                this.mediaLv.DataContext = singViewModel;
                if (singViewModel.SingList.Count > 0 && me.Source==null)
                {
                    mediaLv.SelectedIndex = 0;
                    //me.Source = new Uri(fileNames[0],UriKind.Absolute);
                }
            }
        }

        private void mediaLv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            me.Stop();
            timer.Stop();
            SingInfo singInfo = mediaLv.SelectedItem as SingInfo;
            me.Source = new Uri(Path.Combine(singInfo.SingLocation, singInfo.SingName), UriKind.Absolute);
            me.Play();
            timer.Start();
            playBtn.Content = "暂停";
            //me.ToolTip = "单击暂停";
            SetTime();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //double maxHeight = rightMainDockPanel.ActualHeight - 150;
            //maxHeight = (myDockPanel.ActualWidth / 1.5) > maxHeight ? maxHeight : myDockPanel.ActualWidth / 1.5;
            //MediaConfigInfo mediaConfig = new MediaConfigInfo() { Width = myDockPanel.ActualWidth, Height = maxHeight };
            //this.bindBorder.DataContext = mediaConfig;
        }

        /// <summary>
        /// 媒体控件自适应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double maxHeight =rightMainDockPanel.ActualHeight - 150;
            maxHeight = (myDockPanel.ActualWidth / 1.5) > maxHeight ? maxHeight : myDockPanel.ActualWidth / 1.5;
            MediaConfigInfo mediaConfig = new MediaConfigInfo() { Width = myDockPanel.ActualWidth - 20, Height = maxHeight };
            this.bindBorder.DataContext = mediaConfig;

            CanvasInfo playPointInfo = new CanvasInfo() { Left = 10, Top = maxHeight + 10 };

            CanvasInfo playStatusPointInfo = new CanvasInfo() { Left = 10, Top = maxHeight + playInfo.ActualHeight + 10 };


            CanvasInfo playImgPointInfo = new CanvasInfo() { Left = rightMainDockPanel.ActualWidth/2, Top = (maxHeight-10)/2};

            this.playInfo.DataContext = playPointInfo;
            this.playBtnInfo.DataContext = playStatusPointInfo;
            this.playStackPanel.DataContext = playImgPointInfo;
        }


 

        /// <summary>
        /// 点击播放图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void playStackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SetPlayer(true);
            PlayOrPause();
        }
    }

}